package com.unitd.base.module.master.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unitd.base.module.master.pojo.dto.CategoryVO;
import com.unitd.base.module.master.mybatis.mapper.CategoryMapper;
import com.unitd.base.module.master.pojo.po.CategoryPO;
import com.unitd.base.module.master.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品分类信息管理业务逻辑处理接口实现类
 *
 * @author Category
 * @version 1.0
 * @filename CategoryServiceImpl.java
 * @copyright www.pactera.com
 * @date 2020-08-13
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryPO> implements CategoryService {

    /**
     * 根据产品分类id获取该分类详情
     *
     * @param id 分类id
     * @return 分类详情
     */
    @Override
    public CategoryVO selectById(Long id) {
        CategoryPO po = baseMapper.selectById(id);
        return convertPoToVo(po);
    }

    /**
     * 根据查询条件map得到相应的商品分类信息
     *
     * @param columnMap 查询条件
     * @return 商品分类信息
     */
    @Override
    public List<CategoryVO> selectByMap(Map<String, Object> columnMap) {
        List<CategoryPO> pos = baseMapper.selectByMap(columnMap);
        return pos.stream().map(this::convertPoToVo).collect(Collectors.toList());
    }

    /**
     * 将po对象转换成vo对象
     *
     * @param po 数据库实体对象
     * @return 数据交互、展示对象
     */
    private CategoryVO convertPoToVo(CategoryPO po) {

        CategoryVO vo = new CategoryVO();
        BeanUtils.copyProperties(po, vo);

        vo.setFront_name(po.getFrontName())
                .setFront_desc(po.getFrontDesc())
                .setParent_id(po.getParentId())
                .setSort_order(po.getSortOrder())
                .setShow_index(po.getShowIndex())
                .setBanner_url(po.getBannerUrl())
                .setIcon_url(po.getIconUrl())
                .setImg_url(po.getImgUrl())
                .setWap_banner_url(po.getWapBannerUrl());
        return vo;
    }
}